package getOffer.offer39;

/**
 * @Author: wjy
 * @Date: 2021/11/17 16:02
 */
public class Solution {

    public static void main(String[] args) {

        int[] ints = {1, 2, 3, 2, 2, 2, 5, 4, 2};

        Solution solution = new Solution();
        System.out.println(solution.majorityElement(ints));

    }

    /**
     * 选举法
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        int votes = 0, x = 0;
        for (int i : nums) {
            if (votes == 0) {
                x = i;
            }
            votes += i == x ? 1 : -1;
        }
        return x;
    }

}
